// manager/pages/persondesc/index.js
import config from '../../../utils/config';
import { request } from '../../../utils/request';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        baseUrl: config.baseUrl,
        type: "",
        dict: {},
        openList: [],
        keynoteList: [],
        // 企业标签
        tag: [],
        // 客户层级
        level: [],
        // 个人信息
        personId: "",
        personDetail: {}
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            personId: options.personId,
            type: options.type
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.getPersonInfo();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },
    /**
     * 修改信息
     */
    editForm(){
        wx.navigateTo({
          url: `/manager/pages/editperson/index?personId=${this.data.personId}`,
        })
    },
    // 个人信息
    getPersonInfo(){
        request({
            url: "/personMarketing/getPersonInfo",
            reqRSAENCRY: true, // 参数是否加密
            resRSADECRY: true, // 返回值是否解密
            data: {
                id: this.data.personId
            }
        }).then(res => {
            if(res.code == 0){
                this.setData({
                    personDetail: res.data
                })
                this.getDictionaryInfo('ecard_person_type');
                this.getDictionaryInfo('common_yes_no');
                this.getDictionaryInfo('ecard_person_marital');
                this.getDictionaryInfo('ecard_person_residential');
                this.getDictionaryInfo('ecard_agriculture_relationship');
                this.getDictionaryInfo('ecard_person_business_project');
                this.getDictionaryInfo('ecard_person_business_tag');
                this.getDictionaryInfo('ecard_person_tag');
                this.getDictionaryInfo('ecard_person_level');
            }
        }).catch(error => {
            // console.log(error)
        })
    },

    // 查看图片
    previewImage(e) {
        let { url } = e.currentTarget.dataset;
        wx.previewImage({
            current: url, // 当前显示图片的http链接
            urls: [url] // 需要预览的图片http链接列表
        })
    },

    /**
     * 获取所属行业、营销重点字典
     */
    /**
     * 获取字典
     */
    getDictionaryInfo(dictType) {
        request({
            url: "/commonApi/getDictionaryInfo",
            method: "GET",
            data: {
                dictType
            }
        }).then(res => {
            if(res.code == 0){
                this.setData({
                    ['dict.'+dictType]: res.data
                })
                if(dictType == 'ecard_person_business_tag'){
                    this.getOpenAndKeynoteList(res.data);
                }
                if(dictType == 'ecard_person_tag'){
                    this.getTag(res.data);
                }
                if(dictType == 'ecard_person_level'){
                    this.getLevel(res.data);
                }
            }
        }).catch(error => {
            // console.log(error)
        })
    },

    // 获取已开通、营销重点
    getOpenAndKeynoteList(list){
        let openList = [];
        let keynoteList = [];
        let barr = this.data.personDetail.perBusinessLabel?this.data.personDetail.perBusinessLabel.split(","):[];
        let farr = this.data.personDetail.perMarketingFocus?this.data.personDetail.perMarketingFocus.split(","):[];
        list.forEach(item => {
            if(barr.indexOf(item.dictValue)!=-1){
                openList.push(item)
            }
            if(farr.indexOf(item.dictValue)!=-1){
                keynoteList.push(item)
            }
        })
        this.setData({
            openList,
            keynoteList
        })
    },
    // 获取企业标签
    getTag(list){
        let tag = [];
        let arr = this.data.personDetail.perLabel?this.data.personDetail.perLabel.split(","):[];
        list.forEach(item => {
            if(arr.indexOf(item.dictValue)!=-1){
                tag.push(item)
            }
        })
        this.setData({
            tag
        })
    },
    // 获取客户层级
    getLevel(list){
        let level = [];
        let arr = this.data.personDetail.perLevel?this.data.personDetail.perLevel.split(","):[];
        list.forEach(item => {
            if(arr.indexOf(item.dictValue)!=-1){
                level.push(item)
            }
        })
        this.setData({
            level
        })
    },
})